/**
 * @description 连续字符
 * @author 李子一
 */

interface IRes {
	char: string
	length: number
}

// 嵌套循环
export function findContinuousChar1(str: string): IRes {
	const res: IRes = { char: '', length: 0 }
	const len = str.length
	if (!len) return res

	let tempLength = 0
	for (let i = 0; i < len; i++) {
		tempLength = 0
		for (let j = i; j < len; j++) {
			if (str[i] === str[j]) {
				tempLength++
			}

			if (str[i] !== str[j] || j === len - 1) {
				if (tempLength > res.length) {
					res.char = str[i]
					res.length = tempLength
				}

				if (i < len - 1) {
					i = j - 1
				}

				break
			}
		}
	}
	return res
}

// 双指针
export function findContinuousChar2(str: string): IRes {
	const res: IRes = { char: '', length: 0 }
	const len = str.length
	if (!len) return res

	let tempLength = 0
	let j = 0
	for (let i = 0; i < len; i++) {
		if (str[i] === str[j]) {
			tempLength++
		}
		if (str[i] !== str[j] || i === len - 1) {
			if (tempLength > res.length) {
				res.char = str[j]
				res.length = tempLength
			}
			tempLength = 0

			if (i < len - 1) {
				j = i
				i--
			}
		}
	}
	return res
}
